<?php

namespace MoChat\App\Client\Service;

use MoChat\App\Client\Contract\ClientTagGroupContract;
use MoChat\App\Client\Model\ClientTagGroup;
use MoChat\Framework\Service\AbstractService;

class ClientTagGroupService extends AbstractService implements ClientTagGroupContract
{
    /**
     * @var ClientTagGroup
     */
    protected $model;

    /**
     * 查询多条 - 根据企业ID.
     * @param array $corpId 企业ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getWorkContactTagGroupsByCorpId(array $corpId, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->whereIn('corp_id', $corpId)
            ->get($columns);

        if (empty($res)) {
            return [];
        }

        return $res->toArray();
    }

    public function getClientTagGroupById(int $id, array $columns = ['*']): array
    {
        return $this->model->getOneById($id, $columns);
    }

    /**
     * 查询多条 - 根据分组名称.
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getWorkContactTagGroupsByName(int $corpId, string $groupName, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('group_name', $groupName)
            ->where('corp_id', $corpId)
            ->orderBy('is_default','desc')
            ->get($columns);

        if (empty($res)) {
            return [];
        }

        return $res->toArray();
    }

    /**
     * 查询条数.
     */
    public function countWorkContactTagGroup(int $corpId): int
    {
        return $this->model::query()
            ->where('corp_id', $corpId)
            ->count();
    }

    /**
     * 添加单条
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createWorkContactTagGroup(array $data): int
    {
        return $this->model->createOne($data);
    }

    /**
     * 查询分组标签数量.
     */
    public function countWorkContactTagByGroupId(int $groupId): int
    {
        return $this->model::query()->where('contact_tag_group_id', $groupId)->count();
    }

    /**
     * 添加多条
     * @param array $data 添加的数据
     * @return bool 执行结果
     */
    public function createWorkContactTags(array $data): bool
    {
        return $this->model->createAll($data);
    }

    /**
     * 查询多条 - 根据ID.
     * @param array $ids ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getWorkContactTagGroupsById(array $ids, array $columns = ['*']): array
    {
        $data= $this->model->orderBy('is_default','desc')->whereIn('id',$ids)->get($columns);
        $data || $data = collect([]);
        return $data->toArray();
    }

    /**
     * 查询多条 - 根据企业ID.
     * @param array $corpId 企业ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getWorkContactTagGroupsByCorpIdOrder(array $corpId, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->whereIn('corp_id', $corpId)
            ->orderByRaw('`is_default` desc,`order` asc, `id` desc')
            ->get($columns);

        if (empty($res)) {
            return [];
        }

        return $res->toArray();
    }

    /**
     * 查询多条 - 根据分组名称.
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getWorkContactTagGroupsByNameId(int $corpId, string $groupName, int $id, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('group_name', $groupName)
            ->where('corp_id', $corpId)
            ->where('id', '<>', $id)
            ->get($columns);

        $res || $res = collect([]);
        return $res->toArray();
    }

    /**
     * 查询单条 - 根据ID.
     * @param int $id ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getWorkContactTagGroupById(int $id, array $columns = ['*']): array
    {
        return $this->model->getOneById($id, $columns);
    }

    /**
     * 修改单条 - 根据ID.
     * @param int $id id
     * @param array $data 修改数据
     * @return int 修改条数
     */
    public function updateWorkContactTagGroupById(int $id, array $data): int
    {
        return $this->model->updateOneById($id, $data);
    }

    /**
     * 删除 - 单条
     * @param int $id 删除ID
     * @return int 删除条数
     */
    public function deleteWorkContactTagGroup(int $id): int
    {
        return $this->model->deleteOne($id);
    }

    /**
     * 通过条件获取单条数据
     * @param $where
     * @param array $filed
     * @return array|null
     */
    public function getOneWorkContactTagGroupByWhere($where, array $filed = ['*']): ?array
    {
        $info = $this->model->where($where)->first();
        return empty($info) ? null : $info->toArray();
    }
}